﻿using System;
using System.Data;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using UCOLOR.Data;

namespace UCOLOR.Extend
{
    public class DataToObjet<T> where T : BaseModel
    {

        /// <summary>
        /// 通过查询结果，返回实体数组;
        /// </summary>
        /// <param name="dt">查询结果</param>
        /// <returns>实体对象</returns>
        public IList<T> DatatableToObject(DataTable dt)
        {
            if (dt.Rows.Count == 0)
            {
                return null;
            }

            T obj = Activator.CreateInstance<T>();
            Type t = obj.GetType();
            PropertyInfo[] Mypropertyinfos = t.GetProperties();
            IList<T> al = new List<T>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                T _obj = (T)System.Reflection.Assembly.GetAssembly(t).CreateInstance(t.ToString());
                foreach (PropertyInfo pi in Mypropertyinfos)
                {
                    if (pi.Name.IndexOf("iSun_") == 0)
                        continue;
                    if (dt.Columns[pi.Name] == null)
                        continue;
                    try
                    {
                        if (dt.Rows[i].ItemArray[dt.Columns[pi.Name].Ordinal] != null)
                        {
                            SetPropertyValue(_obj, pi.Name, dt.Rows[i].ItemArray[dt.Columns[pi.Name].Ordinal]);
                        }
                        else
                        {
                            SetPropertyValue(_obj, pi.Name, null);
                        }
                    }
                    catch (System.Exception e)
                    {
                        //log
                        //throw e;
                        Console.WriteLine(e.StackTrace);
                    }
                }
                al.Add(_obj);
            }
            return al;
        }

        #region 动态获取和设置相关对象的属性的值
        /// <summary>
        /// 获取对象属性的值
        /// </summary>
        /// <param name="obj">Obj the type is object</param>
        /// <param name="name">name is Preperty name</param>
        /// <returns></returns>
        public static object GetPropertyValue(object obj, string name)
        {
            object obj1 = null;
            try
            {
                obj1 = obj.GetType().InvokeMember(name, BindingFlags.GetProperty, null, obj, null);
            }
            catch
            {
            }
            return obj1;
        }

        /// <summary>
        /// 如果某个object有名为PropertyName的属性，则获取此属性值
        /// 如果没有，则返回null
        /// </summary>
        private static string GetObjectProperty(object obj, string propertyName)
        {
            try
            {
                if (obj == null)
                    return null;
                else
                {
                    PropertyInfo p = obj.GetType().GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance);
                    if (p == null)
                        return null;
                    else
                        return Convert.ToString(p.GetValue(obj, null));
                }

            }
            catch
            {
                return null;
            }
        }
        /// <summary>
        /// 设置实体对象的属性的值
        /// </summary>
        /// <param name="obj">实体对象</param>
        /// <param name="name">实体对象的属性</param>
        /// <param name="value">要设置的值</param>
        public static void SetPropertyValue(object obj, string name, object value)
        {
            try
            {
                object[] objArray1 = new object[1];
                Type type1 = obj.GetType().GetProperty(name).PropertyType;
                if (value.GetType() == typeof(string))
                {
                    value = value.ToString().Replace("[B]", "'");
                }
                objArray1[0] = ChangeTypes(value, type1);
                obj.GetType().InvokeMember(name, BindingFlags.SetProperty, null, obj, objArray1);
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
        }

        public static Type GetPropertyType(object obj, string name)
        {
            return obj.GetType().GetProperty(name).PropertyType;
        }

        public static object ChangeTypes(object value, Type conversionType)
        {
            if (conversionType.IsGenericType &&
                conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
            {
                if (value == null)
                    return null;

                System.ComponentModel.NullableConverter nullableConverter
                    = new System.ComponentModel.NullableConverter(conversionType);

                conversionType = nullableConverter.UnderlyingType;
            }
            if (conversionType.ToString().IndexOf("Guid") > 0)
            {
                value = new Guid(value.ToString());
            }
            return Convert.ChangeType(value, conversionType);
        }

        /// <summary>
        /// 获取属性的值
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static T GetPropertyValue<T>(object obj, string name)
        {
            try
            {
                return (T)GetPropertyValue(obj, name);
            }
            catch (System.Exception e)
            {
                throw e;
            }
        }


        public static Type GetProgertyType(string name, object obj)
        {
            return GetPropertyType(obj, name);
        }
        #endregion

       
    }
}
